﻿Imports System.Data
Partial Public Class FrmProductBill
#Region "新建单据"
    Private Sub NewBill()
        GetBaseList()
        '-全部清空
        FBInfo = New BServer.FBillProduct
        FDateBox.Value = Now.Date
        FBillNoBox.Text = String.Empty
        FVerifyBox.Text = String.Empty

        FUnit1Box.Text = String.Empty
        FUnit2Box.Text = String.Empty
        FUnit3Box.Text = String.Empty
        FUnit4Box.Text = String.Empty
        FUnit5Box.Text = String.Empty

        FQty1Box.Value = 0
        FQty2Box.Value = 0
        FQty3Box.Value = 0
        FQty4Box.Value = 0
        FQty5Box.Value = 0

        FPrice1Box.Value = 0
        FPrice2Box.Value = 0
        FPrice3Box.Value = 0
        FPrice4Box.Value = 0
        FPrice5Box.Value = 0

        FAcc1Box.Value = 0
        FAcc2Box.Value = 0
        FAcc3Box.Value = 0
        FAcc4Box.Value = 0
        FAcc5Box.Value = 0

        FAmountBigLabel.Text = "零元"
        FAmountLabel.Text = "0.00"
        FMakerBox.Text = UInfo.FRName
        FVermanBox.Text = String.Empty
        FSaveBtn.Enabled = True
        FSaveBtn.Image = My.Resources.saveas
        FSaveBtn.Text = "保存"
        FPrintBtn.Enabled = False
        CHGControls(True)
    End Sub
#End Region
#Region "取基础资料"
    Private Sub GetBaseList()
        Try
            Dim ErrString As String = String.Empty
            Dim TmpArr As Byte() = Nothing
            Dim ds As DataSet = Nothing
            If SControl._BILL_Product_GetBaseList(UInfo.FSession, 0, "*", TmpArr, ErrString) = False Then
                MsgBox(ErrString)
                Return
            End If
            ds = New DataSet
            If FControl.DsRestore(ds, TmpArr) = False Then
                MsgBox("无法解析返回数据!")
                Return
            End If
            FNameBox.DataSource = ds.Tables(0).DefaultView
            FCNameBox.DataSource = ds.Tables(0).Copy.DefaultView
            FNameBox.ValueMember = "id"
            FNameBox.DisplayMember = "FName"
            FCNameBox.ValueMember = "id"
            FCNameBox.DisplayMember = "FCName"
            FNameBox.SelectedIndex = -1
            FCNameBox.SelectedIndex = -1
            '--项目
            TmpArr = Nothing
            ds = New DataSet
            If SControl._BILL_Product_GetBaseList(UInfo.FSession, 1, "*", TmpArr, ErrString) = False Then
                MsgBox(ErrString)
                Return
            End If
            If FControl.DsRestore(ds, TmpArr) = False Then
                MsgBox("无法解析返回数据!")
                Return
            End If
            FItem1Box.DataSource = ds.Tables(0).DefaultView
            FItem1Box.ValueMember = "id"
            FItem1Box.DisplayMember = "FName"
            FItem1Box.SelectedIndex = -1

            FItem2Box.DataSource = ds.Tables(0).Copy.DefaultView
            FItem2Box.ValueMember = "id"
            FItem2Box.DisplayMember = "FName"
            FItem2Box.SelectedIndex = -1

            FItem3Box.DataSource = ds.Tables(0).Copy.DefaultView
            FItem3Box.ValueMember = "id"
            FItem3Box.DisplayMember = "FName"
            FItem3Box.SelectedIndex = -1

            FItem4Box.DataSource = ds.Tables(0).Copy.DefaultView
            FItem4Box.ValueMember = "id"
            FItem4Box.DisplayMember = "FName"
            FItem4Box.SelectedIndex = -1

            FItem5Box.DataSource = ds.Tables(0).Copy.DefaultView
            FItem5Box.ValueMember = "id"
            FItem5Box.DisplayMember = "FName"
            FItem5Box.SelectedIndex = -1
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
#End Region
#Region "保存单据"
    Private Sub SaveBill()
        '---检查必录数据
        If FDateBox.Value = Nothing Then
            MsgBox("日期必须填写!")
            Return
        End If
        If FNameBox.Text = String.Empty Then
            MsgBox("交款单位必须填写!")
            Return
        End If
        If FItem1Box.Text.Trim = String.Empty Then
            MsgBox("第一行分录不能为空!")
            Return
        End If
        If FQty1Box.Value = 0 Then
            MsgBox("第一行数量不能为空!")
            Return
        End If
        If FPrice1Box.Value = 0 Then
            MsgBox("第一行单价不能为空!")
            Return
        End If
        If FAcc1Box.Value = 0 Then
            MsgBox("第一行金额不正确!")
            Return
        End If

        '--第二行
        If FItem2Box.Text.Trim <> String.Empty Then
            If FQty2Box.Value = 0 Then
                MsgBox("第二行数量不能为空!")
                Return
            End If
            If FPrice2Box.Value = 0 Then
                MsgBox("第二行单价不能为空!")
                Return
            End If
            If FAcc2Box.Value = 0 Then
                MsgBox("第二行金额不正确!")
                Return
            End If
        End If

        '--第三行
        If FItem3Box.Text.Trim <> String.Empty Then
            If FQty3Box.Value = 0 Then
                MsgBox("第三行数量不能为空!")
                Return
            End If
            If FPrice3Box.Value = 0 Then
                MsgBox("第三行单价不能为空!")
                Return
            End If
            If FAcc3Box.Value = 0 Then
                MsgBox("第三行金额不正确!")
                Return
            End If
        End If
        '--第四行
        If FItem4Box.Text.Trim <> String.Empty Then
            If FQty4Box.Value = 0 Then
                MsgBox("第四行数量不能为空!")
                Return
            End If
            If FPrice4Box.Value = 0 Then
                MsgBox("第四行单价不能为空!")
                Return
            End If
            If FAcc4Box.Value = 0 Then
                MsgBox("第四行金额不正确!")
                Return
            End If
        End If

        '--第五行
        If FItem5Box.Text.Trim <> String.Empty Then
            If FQty5Box.Value = 0 Then
                MsgBox("第五行数量不能为空!")
                Return
            End If
            If FPrice5Box.Value = 0 Then
                MsgBox("第五行单价不能为空!")
                Return
            End If
            If FAcc5Box.Value = 0 Then
                MsgBox("第五行金额不正确!")
                Return
            End If
        End If
        Try
            Dim ErrString As String = String.Empty
            FBInfo.FDate = FDateBox.Value
            If FBInfo.id = Nothing Then
                '--新单据保存
                '-检查客户是否存在
                Dim s As Integer = FNameBox.FindStringExact(FNameBox.Text)
                If s < 0 Then
                    '--不存在,FCID写-1
                    FBInfo.FCid = -1
                    FBInfo.FName = FNameBox.Text
                    FBInfo.FCName = FCNameBox.Text
                Else
                    '--写FCID
                    FBInfo.FCid = FNameBox.SelectedValue
                    FBInfo.FName = FNameBox.Text
                    FBInfo.FCName = FCNameBox.Text
                End If
                '--组合结构体
                If FItem1Box.Text <> String.Empty Then
                    s = FItem1Box.FindStringExact(FItem1Box.Text)
                    If s >= 0 Then
                        '--找到相应ID,填写FITEMID
                        If FItem1Box.SelectedIndex <> s Then
                            FItem1Box.SelectedIndex = s
                        End If
                        FBInfo.FItem1 = FItem1Box.SelectedValue
                        FBInfo.FItemName1 = FItem1Box.Text.Trim
                    Else
                        '--FItemid写-1,直接写名称
                        FBInfo.FItem1 = -1
                        FBInfo.FItemName1 = FItem1Box.Text.Trim
                    End If

                    If FQty1Box.Value = 0 Then
                        MsgBox("数量不能为0")
                        Return
                    End If
                    If FPrice1Box.Value = 0 Then
                        MsgBox("单价不能为0")
                        Return
                    End If
                    FBInfo.FUnit1 = FUnit1Box.Text
                    FBInfo.FQty1 = FQty1Box.Value
                    FBInfo.FPrice1 = FPrice1Box.Value
                    FBInfo.FAcc1 = FAcc1Box.Value
                    FBInfo.FMark1 = FMark1Box.Text
                End If

                '-第二行
                If FItem2Box.Text <> String.Empty Then
                    s = FItem2Box.FindStringExact(FItem2Box.Text)
                    If s >= 0 Then
                        '--找到相应ID,填写FITEMID
                        If FItem2Box.SelectedIndex <> s Then
                            FItem2Box.SelectedIndex = s
                        End If
                        FBInfo.FItem2 = FItem2Box.SelectedValue
                        FBInfo.FItemName2 = FItem2Box.Text.Trim
                    Else
                        '--FItemid写-1,直接写名称
                        FBInfo.FItem2 = -1
                        FBInfo.FItemName2 = FItem2Box.Text.Trim
                    End If

                    If FQty2Box.Value = 0 Then
                        MsgBox("数量不能为0")
                        Return
                    End If
                    If FPrice2Box.Value = 0 Then
                        MsgBox("单价 不能为空")
                        Return
                    End If
                    FBInfo.FUnit2 = FUnit2Box.Text
                    FBInfo.FQty2 = FQty2Box.Value
                    FBInfo.FPrice2 = FPrice2Box.Value
                    FBInfo.FAcc2 = FAcc2Box.Value
                    FBInfo.FMark2 = FMark2Box.Text
                End If

                '-第三行
                If FItem3Box.Text <> String.Empty Then
                    s = FItem3Box.FindStringExact(FItem3Box.Text)
                    If s >= 0 Then
                        '--找到相应ID,填写FITEMID
                        If FItem3Box.SelectedIndex <> s Then
                            FItem3Box.SelectedIndex = s
                        End If
                        FBInfo.FItem3 = FItem3Box.SelectedValue
                        FBInfo.FItemName3 = FItem3Box.Text.Trim
                    Else
                        '--FItemid写-1,直接写名称
                        FBInfo.FItem3 = -1
                        FBInfo.FItemName3 = FItem3Box.Text.Trim
                    End If
                    If FQty3Box.Value = 0 Then
                        MsgBox("数量不能为0")
                        Return
                    End If
                    If FPrice3Box.Value = 0 Then
                        MsgBox("单价 不能为空")
                        Return
                    End If
                    FBInfo.FUnit3 = FUnit3Box.Text
                    FBInfo.FQty3 = FQty3Box.Value
                    FBInfo.FPrice3 = FPrice3Box.Value
                    FBInfo.FAcc3 = FAcc3Box.Value
                    FBInfo.FMark3 = FMark3Box.Text
                End If

                '-第四行
                If FItem4Box.Text <> String.Empty Then
                    s = FItem4Box.FindStringExact(FItem4Box.Text)
                    If s >= 0 Then
                        '--找到相应ID,填写FITEMID
                        If FItem4Box.SelectedIndex <> s Then
                            FItem4Box.SelectedIndex = s
                        End If
                        FBInfo.FItem4 = FItem4Box.SelectedValue
                        FBInfo.FItemName4 = FItem4Box.Text.Trim
                    Else
                        '--FItemid写-1,直接写名称
                        FBInfo.FItem4 = -1
                        FBInfo.FItemName4 = FItem4Box.Text.Trim
                    End If
                    If FQty4Box.Value = 0 Then
                        MsgBox("数量不能为0")
                        Return
                    End If
                    If FPrice4Box.Value = 0 Then
                        MsgBox("单价不能为空")
                        Return
                    End If
                    FBInfo.FUnit4 = FUnit4Box.Text
                    FBInfo.FQty4 = FQty4Box.Value
                    FBInfo.FPrice4 = FPrice4Box.Value
                    FBInfo.FAcc4 = FAcc4Box.Value
                    FBInfo.FMark4 = FMark4Box.Text
                End If
                '-第五行
                If FItem5Box.Text <> String.Empty Then
                    s = FItem5Box.FindStringExact(FItem5Box.Text)
                    If s >= 0 Then
                        '--找到相应ID,填写FITEMID
                        If FItem5Box.SelectedIndex <> s Then
                            FItem5Box.SelectedIndex = s
                        End If
                        FBInfo.FItem5 = FItem5Box.SelectedValue
                        FBInfo.FItemName5 = FItem5Box.Text.Trim
                    Else
                        '--FItemid写-1,直接写名称
                        FBInfo.FItem5 = -1
                        FBInfo.FItemName5 = FItem5Box.Text.Trim
                    End If
                    If FQty5Box.Value = 0 Then
                        MsgBox("数量不能为0")
                        Return
                    End If
                    If FPrice5Box.Value = 0 Then
                        MsgBox("单价不能为空")
                        Return
                    End If
                    FBInfo.FMark5 = FMark5Box.Text
                    FBInfo.FUnit5 = FUnit5Box.Text
                    FBInfo.FQty5 = FQty5Box.Value
                    FBInfo.FPrice5 = FPrice5Box.Value
                    FBInfo.FAcc5 = FAcc5Box.Value
                End If

                FBInfo.FAmount = CDbl(FAmountLabel.Text)
                FBInfo.FUid = UInfo.id
                FBInfo.FUName = UInfo.FRName
                If SControl._BILL_Product_SaveBill(UInfo.FSession, FBInfo, ErrString) = False Then
                    MsgBox(ErrString)
                    Return
                End If
                CHGControls(False)
                '--取单号和验证码
                If FBInfo.FVerifyCode Is Nothing Or FBInfo.FBillNo Is Nothing Then
                    MsgBox("取单据号码和验证码失败!未知错误")
                    Return
                End If
                '-写单号和验证码
                FBillNoBox.Text = FBInfo.FBillNo
                FVerifyBox.Text = FBInfo.FVerifyCode
                FSaveBtn.Enabled = False
                If SControl._User_CheckAuth(UInfo.FSession, "BILL", "FUBILLVERIFYMY", "") Then
                    FSaveBtn.Enabled = True
                    FSaveBtn.Image = My.Resources.money
                    FSaveBtn.Text = "收费"
                End If
            ElseIf FBInfo.FVid = Nothing And FBInfo.id > 0 Then
                '--审核
                FBInfo.FVid = UInfo.id
                FBInfo.FVName = UInfo.FRName
                If SControl._BILL_Product_SaveBill(UInfo.FSession, FBInfo, ErrString) = False Then
                    MsgBox(ErrString)
                    Return
                End If
                FVermanBox.Text = FBInfo.FVName
                MsgBox("收费成功!")
                FSaveBtn.Enabled = False
                FPrintBtn.Enabled = True
                Return
            Else
                MsgBox("参数错误!")
                Return
            End If
        Catch ex As Exception
            MsgBox(String.Format("保存单据时出现错误!{0}错误信息:{1}", vbCrLf, ex.Message))
        End Try
    End Sub
#End Region
#Region "取单据信息"
    Private Function GetBillInfo() As Boolean
        FBInfo = New BServer.FBillProduct
        Try
            Dim ErrString As String = String.Empty
            If SControl._BILL_Product_GetBillInfo(UInfo.FSession, FBid, "", FBInfo, ErrString) = False Then
                MsgBox(ErrString)
                Return False
            End If
            FDateBox.Value = FBInfo.FDate
            FNameBox.Text = FBInfo.FName
            FCNameBox.Text = FBInfo.FCName
            FBillNoBox.Text = FBInfo.FBillNo
            FVerifyBox.Text = FBInfo.FVerifyCode

            FItem1Box.Text = FBInfo.FItemName1
            FQty1Box.Value = FBInfo.FQty1
            FPrice1Box.Value = FBInfo.FPrice1
            FUnit1Box.Text = FBInfo.FUnit1
            FAcc1Box.Value = FBInfo.FAcc1
            FMark1Box.Text = FBInfo.FMark1

            FItem2Box.Text = FBInfo.FItemName2
            FQty2Box.Value = FBInfo.FQty2
            FPrice2Box.Value = FBInfo.FPrice2
            FUnit2Box.Text = FBInfo.FUnit2
            FAcc2Box.Value = FBInfo.FAcc2
            FMark2Box.Text = FBInfo.FMark2

            FItem3Box.Text = FBInfo.FItemName3
            FQty3Box.Value = FBInfo.FQty3
            FPrice3Box.Value = FBInfo.FPrice3
            FUnit3Box.Text = FBInfo.FUnit3
            FAcc3Box.Value = FBInfo.FAcc3
            FMark3Box.Text = FBInfo.FMark3

            FItem4Box.Text = FBInfo.FItemName4
            FQty4Box.Value = FBInfo.FQty4
            FPrice4Box.Value = FBInfo.FPrice4
            FUnit4Box.Text = FBInfo.FUnit4
            FAcc4Box.Value = FBInfo.FAcc4
            FMark4Box.Text = FBInfo.FMark4

            FItem5Box.Text = FBInfo.FItemName5
            FQty5Box.Value = FBInfo.FQty5
            FPrice5Box.Value = FBInfo.FPrice5
            FUnit5Box.Text = FBInfo.FUnit5
            FAcc5Box.Value = FBInfo.FAcc5
            FMark5Box.Text = FBInfo.FMark5

            FAmountLabel.Text = FBInfo.FAmount
            FAmountBigLabel.Text = FControl.MoneyToUpper(FBInfo.FAmount)

            FMakerBox.Text = FBInfo.FUName
            FVermanBox.Text = FBInfo.FVName

            Label1.Text = "打印次数:" & FBInfo.FPCount
            FDateBox.Enabled = False
            FNameBox.Enabled = False
            FCNameBox.Enabled = False

            FQty1Box.Enabled = False
            FPrice1Box.Enabled = False
            FAcc1Box.Enabled = False

            FQty2Box.Enabled = False
            FPrice2Box.Enabled = False
            FAcc2Box.Enabled = False

            FQty3Box.Enabled = False
            FPrice3Box.Enabled = False
            FAcc3Box.Enabled = False

            FQty4Box.Enabled = False
            FPrice4Box.Enabled = False
            FAcc4Box.Enabled = False

            FQty5Box.Enabled = False
            FPrice5Box.Enabled = False
            FAcc5Box.Enabled = False
            Return True
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        End Try
    End Function
#End Region
#Region "设置按钮状态"
    Private Sub CHGControls(ByVal FStat As Boolean)
        FDateBox.Enabled = FStat
        FNameBox.Enabled = FStat
        FCNameBox.Enabled = FStat

        FItem1Box.Enabled = FStat
        FQty1Box.Enabled = FStat
        FPrice1Box.Enabled = FStat

        FItem2Box.Enabled = FStat
        FQty2Box.Enabled = FStat
        FPrice2Box.Enabled = FStat

        FItem3Box.Enabled = FStat
        FQty3Box.Enabled = FStat
        FPrice3Box.Enabled = FStat

        FItem4Box.Enabled = FStat
        FQty4Box.Enabled = FStat
        FPrice4Box.Enabled = FStat

        FItem5Box.Enabled = FStat
        FQty5Box.Enabled = FStat
        FPrice5Box.Enabled = FStat

        FMark1Box.Enabled = FStat
        FMark2Box.Enabled = FStat
        FMark3Box.Enabled = FStat
        FMark4Box.Enabled = FStat
        FMark5Box.Enabled = FStat

        FUnit1Box.Enabled = FStat
        FUnit2Box.Enabled = FStat
        FUnit3Box.Enabled = FStat
        FUnit4Box.Enabled = FStat
        FUnit5Box.Enabled = FStat
    End Sub
#End Region
End Class
